package algotithm.leetcode.tree.jianzhi68;

import algotithm.leetcode.tree.TreeNode;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/26-17:47
 * @URL https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/
 * @Title 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
 */
public class Solution {

    public static void main(String[] args) {

    }

    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode node = root;
        while (true) {
            if (p.val < node.val && q.val > node.val) {
                node = node.left;
            } else if (p.val > node.val && q.val > node.val) {
                node = node.right;
            } else {
                break;
            }
        }
        return node;
    }

}
